# -*- coding: utf-8 -*-

"""
@author: 猿小天
@contact: QQ:1638245306
@Created on: 2021/6/2 002 14:20
@Remark:登录视图
"""
import base64
import hashlib
from datetime import datetime, timedelta

from django.contrib import auth
from django.contrib.auth import login
from django.shortcuts import redirect
from django.utils.translation import gettext_lazy as _
from drf_yasg import openapi
from drf_yasg.utils import swagger_auto_schema
from rest_framework import serializers
from rest_framework.views import APIView
from rest_framework_simplejwt.serializers import TokenObtainPairSerializer
from rest_framework_simplejwt.views import TokenObtainPairView
from django.db.models import Q
from application import settings
from dvadmin.system.models import Users
from dvadmin.utils.json_response import SuccessResponse, ErrorResponse, DetailResponse
from dvadmin.utils.serializers import CustomModelSerializer
from dvadmin.utils.validator import CustomValidationError
from dvadmin.utils.request_util import save_login_log



class LoginSerializer(TokenObtainPairSerializer):
    """
    登录的序列化器:
    重写djangorestframework-simplejwt的序列化器
    """
    # captcha = serializers.CharField(max_length=6)

    class Meta:
        model = Users
        fields = "__all__"
        read_only_fields = ["id"]

    default_error_messages = {"no_active_account": _("账号/密码错误")}
    def validate(self, attrs):
        data = super().validate(attrs)
        data['roles'] = self.user.role.values_list('key',flat=True)
        data['name'] = self.user.name
        data['userId'] = self.user.id
        data["avatar"] = self.user.avatar
        request = self.context.get("request")
        request.user = self.user
        # 记录登录日志
        save_login_log(request=request)
        return {"code": 2000, "msg": "请求成功", "data": data}

class LoginView(TokenObtainPairView):
    """
    登录接口
    """
    serializer_class = LoginSerializer
    permission_classes = []


class LoginTokenSerializer(TokenObtainPairSerializer):
    """
    登录的序列化器:
    """

    class Meta:
        model = Users
        fields = "__all__"
        read_only_fields = ["id"]

    default_error_messages = {
        'no_active_account': _('账号/密码不正确')
    }

    def validate(self, attrs):
        if not getattr(settings, 'LOGIN_NO_CAPTCHA_AUTH', False):
            return {
                "code": 4000,
                "msg": "该接口暂未开通!",
                "data": None
            }
        data = super().validate(attrs)
        data['name'] = self.user.name
        data['userId'] = self.user.id
        return {
            "code": 2000,
            "msg": "请求成功",
            "data": data
        }


class LoginTokenView(TokenObtainPairView):
    """
    登录获取token接口
    """
    serializer_class = LoginTokenSerializer
    permission_classes = []


class LogoutView(APIView):
    def post(self, request):
        return DetailResponse(msg="注销成功")


class ApiLoginSerializer(CustomModelSerializer):
    """接口文档登录-序列化器"""
    username = serializers.CharField()
    password = serializers.CharField()

    class Meta:
        model = Users
        fields = ['username', 'password']


class ApiLogin(APIView):
    """接口文档的登录接口"""
    serializer_class = ApiLoginSerializer
    authentication_classes = []
    permission_classes = []

    def post(self, request):
        username = request.data.get('username')
        password = request.data.get('password')
        user_obj = auth.authenticate(request, username=username,
                                     password=hashlib.md5(password.encode(encoding='UTF-8')).hexdigest())
        if user_obj:
            login(request, user_obj)
            return redirect('/')
        else:
            return ErrorResponse(msg="账号/密码错误")
